Retrospective hotspotting

Recall that the "retrospective hotspot" method has no sense of time (but often a sliding window of time is used, e.g. the last 8 weeks of data before the prediction date) and is a (normally grid based) KDE method.


In [1]:
%matplotlib inline
from common import *
datadir = os.path.join("//media", "disk", "Data")
#datadir = os.path.join("..", "..", "..", "..", "..", "Data")


GDAL_DATA not set and failed to find suitable location...  This is probably not a problem on linux.

In [2]:
south_side, points = load_data(datadir)

Find the masked grid to use

It is definitely worth exploring how changing the offset of the grid affects prediction...


In [3]:
masked_grid = grid_for_south_side()
print("Total number of cells in region:", masked_grid.xextent * masked_grid.yextent)
print("Number of cells which intersect geometry:", np.sum(~masked_grid.mask))


Total number of cells in region: 2058
Number of cells which intersect geometry: 961

In [4]:
fig, ax  = plt.subplots(ncols=2, figsize=(16,8))

for a in ax:
    a.add_patch(descartes.PolygonPatch(south_side, fc="none", ec="Black"))
    a.add_patch(descartes.PolygonPatch(south_side, fc="Blue", ec="none", alpha=0.2))
    xmin, ymin, xmax, ymax = south_side.bounds
    a.set(xlim=[xmin-500,xmax+500], ylim=[ymin-500,ymax+500])

pc = open_cp.plot.patches_from_grid(grid_for_south_side())
ax[0].add_collection(matplotlib.collections.PatchCollection(pc, facecolor="None", edgecolor="black"))
pc = open_cp.plot.patches_from_grid(grid_for_south_side(xoffset=120, yoffset=120))
ax[1].add_collection(matplotlib.collections.PatchCollection(pc, facecolor="None", edgecolor="black"))
ax[0].set_title("Original gridding")
ax[1].set_title("Offset by 120 meters north and east")
None


Comparison between grid based and continuous


In [5]:
cts_pred = retro.RetroHotSpot()
cts_pred.data = points
cts_pred.weight = retro.Quartic(bandwidth = 1000)
cts_risk = cts_pred.predict(end_time = np.datetime64("2011-09-28"))
cts_grid_risk = open_cp.predictors.grid_prediction(cts_risk, masked_grid)

grid_pred = retro.RetroHotSpotGrid(grid=masked_grid)
grid_pred.data = points
grid_pred.weight = retro.Quartic(bandwidth = 1000)
grid_risk = grid_pred.predict(end_time = np.datetime64("2011-09-28"))
grid_risk.mask_with(masked_grid)

In [6]:
fig, ax  = plt.subplots(ncols=2, figsize=(16,8))

for a in ax:
    a.set_aspect(1)
    a.add_patch(descartes.PolygonPatch(south_side, fc="none", ec="Black"))
    
ax[0].pcolormesh(*cts_grid_risk.mesh_data(), cts_grid_risk.intensity_matrix, cmap=yellow_to_red)
ax[0].set_title("Continuous prediction")
ax[1].pcolormesh(*grid_risk.mesh_data(), grid_risk.intensity_matrix, cmap=yellow_to_red)
ax[1].set_title("Grid prediction")
None


Baseline hit rate


In [8]:
evaluator = open_cp.evaluation.HitRateEvaluator(RetroHotSpotEval(masked_grid, points))
evaluator.data = points
result = evaluator.run(time_range(), range(0,51))

In [9]:
frame = to_dataframe(result.rates)

fig, ax = plt.subplots(figsize=(16,6))
(frame[5] * 100).plot(ax=ax)
(frame[10] * 100).plot(ax=ax)
(frame[15] * 100).plot(ax=ax)
ax.set_ylabel("% hit rate")
ax.legend(["{}% Coverage".format(x) for x in [5,10,15]])
None



In [10]:
frame.describe()


Out[10]:
% Coverage 0 1 2 3 4 5 6 7 8 9 ... 41 42 43 44 45 46 47 48 49 50
count 101.0 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 ... 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000 101.000000
mean 0.0 0.058428 0.094211 0.143468 0.182115 0.219540 0.250367 0.275972 0.296387 0.318754 ... 0.786373 0.800066 0.810977 0.819463 0.823896 0.833025 0.841410 0.846518 0.847343 0.858134
std 0.0 0.091692 0.104734 0.127985 0.146259 0.146781 0.153076 0.167002 0.168620 0.178851 ... 0.155703 0.146743 0.144682 0.141394 0.137113 0.136176 0.138409 0.136089 0.136261 0.127632
min 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.250000 0.375000 0.375000 0.375000 0.375000 0.375000 0.375000 0.375000 0.375000 0.375000
25% 0.0 0.000000 0.000000 0.000000 0.083333 0.111111 0.142857 0.166667 0.187500 0.200000 ... 0.714286 0.714286 0.727273 0.750000 0.750000 0.769231 0.777778 0.777778 0.777778 0.800000
50% 0.0 0.000000 0.076923 0.125000 0.166667 0.200000 0.230769 0.263158 0.285714 0.300000 ... 0.800000 0.823529 0.833333 0.833333 0.833333 0.857143 0.857143 0.857143 0.857143 0.857143
75% 0.0 0.111111 0.166667 0.222222 0.285714 0.333333 0.333333 0.375000 0.416667 0.444444 ... 0.888889 0.888889 0.909091 0.909091 0.916667 0.923077 0.941176 1.000000 1.000000 1.000000
max 0.0 0.400000 0.400000 0.500000 0.500000 0.714286 0.800000 0.900000 0.900000 0.900000 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000

8 rows × 51 columns


In [11]:
fig, ax = plt.subplots(figsize=(6,6))

d = frame.describe()
ax.plot(d.columns, d.ix["mean"]*100)
ax.plot(d.columns, d.ix["25%"]*100)
ax.plot(d.columns, d.ix["50%"]*100)
ax.plot(d.columns, d.ix["75%"]*100)
ax.legend()
ax.set(ylabel="% hit rate", xlabel="% coverage")
ax.set_title("Hit rate against coverage")
ax.set(xlim=[0,50], ylim=[0,100])
None


Show how the prediction changes over time


In [12]:
predictions = []
times = list(result.details)
times.sort()
for time in times:
    predictions.append( result.details[time].prediction )

In [ ]:
from matplotlib import animation
matplotlib.rc('animation', html='html5')

In [ ]:
fig, ax = plt.subplots(figsize=(8,8))
ax.set_aspect(1)
ax.add_patch(descartes.PolygonPatch(south_side, fc="none", ec="Black"))

In [ ]:
def animator(n):
    pred = predictions[n]
    ax.collections = []
    mesh = ax.pcolormesh(*pred.mesh_data(), pred.intensity_matrix, cmap=yellow_to_red)
    ax.set_title("Risk density on {}".format(times[n]))
    return mesh

In [ ]:
animation.FuncAnimation(fig, animator, frames=len(predictions), interval=100, blit=False)

Effect of varying the grid

We'll look at how varying the offset of the grid changes the hit rate.

  • To avoid visualising massive amounts of data, we'll look at 5% coverage only

In [7]:
# This takes a long time to run..
with open_cp.pool.PoolExecutor() as executor:
    futures = []
    for xoffset in range(0, 250, 5):
        for yoffset in range(0, 250, 5):
            masked_grid = grid_for_south_side(xoffset, yoffset)
            task = RHS_Eval_Task(masked_grid, points, (xoffset, yoffset))
            futures.append( executor.submit(task) )
            
    grid_effect = {}
    for key, result in open_cp.pool.yield_task_results(futures):
        grid_effect[key] = result.rates

In [16]:
import pickle, lzma
with lzma.open(os.path.join("grid.pic.xz"), "wb") as f:
    pickle.dump(grid_effect, f)

Here's one we prepared earlier


In [20]:
import pickle, lzma
with lzma.open(os.path.join("grid.pic.xz"), "rb") as f:
    grid_effect = pickle.load(f)

In [21]:
def to_mean(result):
    frame = to_dataframe(result)
    return frame.describe()[5]["mean"]

means = { key : to_mean(value) for key, value in grid_effect.items() }

In [23]:
first_axis_keys = set(k[0] for k in means.keys())
second_axis_keys = set(k[1] for k in means.keys())
frame = pd.DataFrame({k:{l:means[(k,l)] for l in second_axis_keys} for k in first_axis_keys})
frame


Out[23]:
0 5 10 15 20 25 30 35 40 45 ... 200 205 210 215 220 225 230 235 240 245
0 0.219540 0.221362 0.224662 0.219466 0.225994 0.223444 0.226008 0.224680 0.223073 0.219714 ... 0.220867 0.219767 0.221518 0.223251 0.222351 0.223251 0.224241 0.221883 0.221718 0.220618
5 0.222290 0.224112 0.220291 0.223912 0.225287 0.223637 0.226201 0.224963 0.221144 0.219006 ... 0.221574 0.219767 0.220289 0.223437 0.223437 0.223133 0.222708 0.221058 0.221011 0.221011
10 0.222670 0.221192 0.221742 0.220917 0.221742 0.225186 0.227131 0.224903 0.220974 0.219544 ... 0.222504 0.221110 0.221220 0.222603 0.222917 0.221577 0.222381 0.219741 0.221391 0.222835
15 0.222038 0.223035 0.219310 0.224204 0.226129 0.226129 0.228074 0.223477 0.224891 0.219151 ... 0.220485 0.219091 0.219908 0.221181 0.220528 0.219503 0.219503 0.219859 0.219859 0.222203
20 0.221213 0.224404 0.220410 0.225811 0.226054 0.226467 0.226954 0.225599 0.226149 0.222236 ... 0.215916 0.216026 0.216843 0.220288 0.221049 0.220024 0.220849 0.219034 0.222203 0.223028
25 0.222863 0.224986 0.226637 0.225811 0.227049 0.226342 0.227537 0.227006 0.226731 0.224134 ... 0.214356 0.217694 0.216940 0.218397 0.218409 0.220224 0.219949 0.222293 0.222128 0.222953
30 0.220478 0.222205 0.223855 0.223030 0.223167 0.224032 0.227584 0.224916 0.225741 0.224134 ... 0.217860 0.214960 0.214830 0.215537 0.216299 0.220224 0.222533 0.221633 0.220643 0.220643
35 0.217490 0.220719 0.223195 0.222782 0.222507 0.222979 0.226484 0.224421 0.223431 0.220586 ... 0.215364 0.214311 0.214091 0.213227 0.218174 0.218793 0.220993 0.220093 0.218993 0.218993
40 0.217717 0.219132 0.220782 0.219682 0.220507 0.221921 0.222951 0.222421 0.221431 0.218998 ... 0.213743 0.212504 0.212991 0.214752 0.216132 0.217261 0.217812 0.216911 0.219083 0.217374
45 0.215566 0.216981 0.216431 0.216431 0.217256 0.218670 0.219700 0.219788 0.218963 0.216366 ... 0.212643 0.209186 0.210003 0.213500 0.212979 0.212979 0.213199 0.213669 0.214081 0.214906
50 0.214623 0.216588 0.218238 0.218238 0.216313 0.218387 0.220820 0.220908 0.220083 0.217486 ... 0.212938 0.211590 0.210993 0.212555 0.212555 0.211096 0.211921 0.215201 0.215613 0.215613
55 0.214267 0.216507 0.215751 0.214101 0.218513 0.219102 0.219492 0.220523 0.221183 0.219576 ... 0.214128 0.208961 0.209690 0.211387 0.210774 0.210774 0.211599 0.214542 0.215092 0.214267
60 0.217073 0.215972 0.213910 0.215091 0.218018 0.217334 0.218942 0.218166 0.218166 0.215458 ... 0.212006 0.210199 0.211340 0.211057 0.211057 0.212425 0.212150 0.215202 0.214542 0.215092
65 0.216507 0.217387 0.214617 0.214617 0.217095 0.216529 0.217016 0.218048 0.218873 0.217266 ... 0.214364 0.212273 0.210645 0.211765 0.210664 0.210664 0.212562 0.215250 0.215957 0.215957
70 0.211289 0.212153 0.209384 0.212095 0.214690 0.214124 0.214612 0.215643 0.215643 0.214861 ... 0.212619 0.210488 0.207847 0.208555 0.208260 0.208260 0.209498 0.209309 0.211996 0.212704
75 0.210582 0.210896 0.212095 0.212095 0.213462 0.214124 0.214612 0.215643 0.214936 0.210779 ... 0.210281 0.211038 0.207847 0.208555 0.208967 0.208260 0.209073 0.207234 0.209875 0.210582
80 0.208568 0.209590 0.210287 0.210287 0.212362 0.211796 0.212991 0.214543 0.212983 0.213026 ... 0.210281 0.209800 0.209910 0.205903 0.206610 0.207022 0.209250 0.208154 0.209529 0.208775
85 0.210221 0.210535 0.209928 0.209928 0.211186 0.210620 0.210207 0.211286 0.213596 0.211989 ... 0.203978 0.209897 0.211421 0.211303 0.208533 0.208946 0.211380 0.208304 0.209679 0.210339
90 0.207746 0.208720 0.211296 0.211296 0.212710 0.210730 0.210867 0.212889 0.214586 0.209788 ... 0.210791 0.210309 0.212246 0.210802 0.209152 0.209565 0.211380 0.208304 0.207204 0.207864
95 0.208461 0.209050 0.208490 0.208490 0.209905 0.209740 0.210977 0.212009 0.212999 0.208427 ... 0.212384 0.209911 0.211848 0.210197 0.207722 0.208135 0.208960 0.206873 0.206433 0.207141
100 0.209922 0.209097 0.206609 0.207316 0.209202 0.211107 0.210694 0.213335 0.209044 0.208056 ... 0.202031 0.212499 0.210252 0.210374 0.210209 0.211034 0.211034 0.208948 0.208555 0.209922
105 0.212409 0.210170 0.206616 0.211761 0.210342 0.210012 0.209600 0.214096 0.213477 0.207332 ... 0.202526 0.209796 0.209198 0.211796 0.210558 0.212043 0.212043 0.209674 0.208987 0.213234
110 0.209427 0.209510 0.208932 0.212751 0.210578 0.210012 0.209971 0.211002 0.208940 0.207489 ... 0.205991 0.210079 0.210189 0.212786 0.211136 0.213611 0.213642 0.210142 0.209564 0.211006
115 0.213399 0.212574 0.215816 0.215340 0.212652 0.210672 0.210631 0.211662 0.206633 0.205733 ... 0.207199 0.206099 0.211355 0.212963 0.212963 0.217560 0.217025 0.212959 0.213859 0.215639
120 0.214470 0.214470 0.219786 0.217236 0.216104 0.210535 0.211112 0.212762 0.207183 0.206283 ... 0.208506 0.205755 0.211837 0.214858 0.216839 0.220021 0.218921 0.213440 0.213240 0.214195
125 0.215884 0.218713 0.218136 0.217236 0.219444 0.215744 0.212927 0.213477 0.208716 0.207108 ... 0.210486 0.211350 0.214956 0.211197 0.216914 0.219507 0.214560 0.214340 0.213075 0.213782
130 0.214234 0.217613 0.215585 0.216067 0.216735 0.204555 0.211167 0.206358 0.206075 0.205458 ... 0.213330 0.212230 0.210792 0.213389 0.211425 0.216922 0.214564 0.209625 0.212839 0.208793
135 0.213684 0.214794 0.212766 0.212216 0.210743 0.205792 0.204142 0.205368 0.208904 0.208287 ... 0.214745 0.211892 0.210792 0.214289 0.214289 0.216214 0.214446 0.208965 0.208840 0.209272
140 0.214601 0.216133 0.210471 0.208751 0.209749 0.206778 0.209749 0.210974 0.211682 0.207104 ... 0.216791 0.216453 0.212838 0.217750 0.217750 0.220108 0.219165 0.211368 0.210818 0.210700
145 0.217017 0.215780 0.214055 0.211579 0.211697 0.209697 0.210456 0.211682 0.212389 0.206711 ... 0.218913 0.217813 0.218925 0.219165 0.218860 0.220942 0.220227 0.215454 0.213254 0.211721
150 0.215619 0.217457 0.216145 0.216970 0.213375 0.210688 0.213123 0.214086 0.213379 0.210781 ... 0.220013 0.218913 0.221049 0.222939 0.220950 0.227337 0.221218 0.217151 0.214519 0.216444
155 0.212279 0.209399 0.209674 0.207749 0.210145 0.209815 0.208271 0.211297 0.211249 0.209642 ... 0.220088 0.220638 0.220190 0.220699 0.224272 0.223172 0.220422 0.215737 0.214637 0.213104
160 0.211171 0.209705 0.208880 0.214734 0.213642 0.214137 0.214243 0.214950 0.213649 0.212718 ... 0.221632 0.220120 0.219941 0.220967 0.221391 0.224304 0.222035 0.212335 0.212586 0.211171
165 0.211096 0.214734 0.212731 0.216385 0.215842 0.214474 0.217692 0.217409 0.215630 0.214368 ... 0.221632 0.220532 0.220972 0.221627 0.226190 0.225885 0.223685 0.219618 0.213411 0.211996
170 0.211847 0.212436 0.212711 0.214376 0.213732 0.215555 0.217233 0.217390 0.220415 0.214533 ... 0.220513 0.220072 0.221173 0.220479 0.224286 0.225425 0.220106 0.213682 0.210004 0.210711
175 0.213937 0.216365 0.215540 0.215430 0.216538 0.214133 0.215646 0.218003 0.220554 0.213065 ... 0.221998 0.219483 0.218658 0.220479 0.221187 0.223957 0.220224 0.214444 0.213619 0.213112
180 0.216458 0.216883 0.216883 0.214957 0.216965 0.219441 0.217064 0.219421 0.218714 0.213464 ... 0.221525 0.219366 0.219806 0.219300 0.220714 0.221539 0.216761 0.213107 0.216337 0.215720
185 0.210801 0.212812 0.214537 0.211952 0.212777 0.213602 0.216490 0.216647 0.213111 0.210266 ... 0.216726 0.216286 0.216726 0.214805 0.216927 0.216220 0.215284 0.213217 0.214195 0.211649
190 0.213379 0.216290 0.216015 0.216180 0.215591 0.216416 0.218518 0.219461 0.218753 0.215732 ... 0.219854 0.219414 0.219147 0.219347 0.221045 0.223245 0.220062 0.216998 0.216962 0.215194
195 0.216110 0.219736 0.218636 0.217811 0.218911 0.217496 0.220148 0.221248 0.221248 0.219641 ... 0.222349 0.221909 0.219991 0.223382 0.224797 0.223382 0.221732 0.215014 0.216942 0.217100
200 0.216844 0.218333 0.219158 0.216683 0.216919 0.216211 0.218863 0.220671 0.220671 0.219889 ... 0.221221 0.221771 0.220514 0.221705 0.223512 0.222098 0.220447 0.215143 0.216365 0.217072
205 0.217689 0.217343 0.218168 0.215496 0.215221 0.215221 0.218581 0.219681 0.218266 0.216659 ... 0.221881 0.221488 0.219807 0.220715 0.220007 0.221658 0.219457 0.216082 0.216082 0.216789
210 0.217689 0.215874 0.215496 0.215496 0.216321 0.216321 0.216459 0.218109 0.218816 0.217209 ... 0.220467 0.219367 0.217992 0.220715 0.220007 0.221658 0.218632 0.216082 0.216082 0.216789
215 0.218451 0.216636 0.215496 0.217477 0.216321 0.216321 0.217078 0.219641 0.219641 0.218034 ... 0.220054 0.219367 0.217992 0.219017 0.220007 0.221658 0.218632 0.216082 0.216082 0.216982
220 0.215221 0.216086 0.218302 0.217477 0.218302 0.216940 0.217510 0.220191 0.217716 0.215559 ... 0.219394 0.218294 0.217992 0.219017 0.219017 0.221658 0.216082 0.216082 0.216082 0.216082
225 0.213841 0.216017 0.216921 0.216096 0.217471 0.218027 0.221622 0.220398 0.219573 0.217470 ... 0.221637 0.218015 0.218538 0.219563 0.222534 0.222119 0.219919 0.217443 0.217443 0.215538
230 0.217471 0.217471 0.219672 0.218296 0.218915 0.219740 0.223854 0.221723 0.218422 0.218053 ... 0.222910 0.220985 0.220388 0.222534 0.222534 0.224274 0.222734 0.219094 0.219994 0.219994
235 0.219992 0.221539 0.224839 0.223093 0.223736 0.220853 0.221030 0.220898 0.220898 0.219290 ... 0.221977 0.221398 0.220801 0.222946 0.222946 0.224686 0.224211 0.223386 0.224211 0.222396
240 0.220542 0.222364 0.221107 0.224083 0.222975 0.222322 0.224267 0.224473 0.224473 0.221628 ... 0.223627 0.221820 0.221930 0.225421 0.225421 0.223696 0.221779 0.221571 0.223936 0.220542
245 0.218184 0.220625 0.221843 0.221725 0.219596 0.221010 0.222955 0.222454 0.223216 0.222803 ... 0.222370 0.220562 0.220378 0.224164 0.224164 0.223429 0.223504 0.222679 0.222090 0.219174

50 rows × 50 columns


In [30]:
data = np.asarray([np.asarray(frame[x]) for x in frame.columns])
data.shape


Out[30]:
(50, 50)

In [43]:
fig, axes = plt.subplots(ncols=2, figsize=(17,6))

ax = axes[0]
ax.hist(data.flatten(), bins=np.linspace(0.195, 0.23, 20))
ax.set_title("Histogram of mean hit rate")
ax.set_xlabel("Man hit rate")
ax.set_ylabel("Count")

ax = axes[1]
mappable = ax.pcolor(data)
ax.set_title("Mean hit rate for offset of grid")
ax.set_xlabel("X offset, times 5m")
ax.set_ylabel("Y offset, times 5m")
cbar = plt.colorbar(mappable)
cbar.set_label("Mean hit rate")
None



In [ ]: